#include "SpriteAnimator.h"


void IAnimator::Play()
{
	AnimationPhase* animPhase = _animations[_currentAnim];

	if(animPhase != NULL)
	{
		animPhase->_isDone = false;
		animPhase->_elapsed = 0.0f;

		switch(animPhase->animationMode)
		{
		case AM_PLAY:
		case AM_FWBOUNCE:
		default:
			animPhase->currentFrame = animPhase->startFrame;
			break;
		case AM_REVERSE:
		case AM_BWBOUNCE:
			animPhase->currentFrame = animPhase->endFrame;
			break;
		}
	}
}


void IAnimator::Pause()
{
	AnimationPhase* animPhase = _animations[_currentAnim];

	if(animPhase != NULL)
	{
		animPhase->_isDone = !animPhase->_isDone;
	}

}


void IAnimator::Stop()
{
	AnimationPhase* animPhase = _animations[_currentAnim];

	if(animPhase != NULL)
	{
		animPhase->_isDone = true;
		animPhase->currentFrame = animPhase->startFrame;
	}
}


void IAnimator::Begin()
{
	AnimationPhase* animPhase = _animations[_currentAnim];

	if(animPhase != NULL)
	{
		animPhase->currentFrame = animPhase->startFrame;
	}
}


void IAnimator::End()
{
	AnimationPhase* animPhase = _animations[_currentAnim];

	if(animPhase != NULL)
	{
		animPhase->currentFrame = animPhase->endFrame;
	}
}


bool IAnimator::IsStopped()
{
	AnimationPhase* animPhase = _animations[_currentAnim];

	if(animPhase != NULL)
	{
		return animPhase->_isDone;
	}

	return true;
}


void IAnimator::AdvanceAnimation(float time, RECT* r)
{
	if(_currentAnim == -1)
	{
		SetRect(r, 0, 0, _srcWidth, _srcHeight);
		return;
	}

	AnimationPhase* animPhase = _animations[_currentAnim];
	if(animPhase != NULL)
	{
		animPhase->_elapsed += time;

		if(!animPhase->_isDone)
		{
			if(animPhase->_elapsed > (1.0f / (float)animPhase->framesPerSec))
			{
				animPhase->_elapsed = 0.0f;

				switch(animPhase->animationMode)
				{
				case AM_PLAY:
					animPhase->currentFrame++;

					if(animPhase->currentFrame > animPhase->endFrame)
					{
						if(animPhase->loop)
						{
							animPhase->currentFrame = animPhase->startFrame;
						}
						else
						{
							animPhase->currentFrame = animPhase->endFrame;
							animPhase->_isDone = true;
						}
					}

					break;
				case AM_REVERSE:
					animPhase->currentFrame--;

					if(animPhase->currentFrame < animPhase->startFrame)
					{
						if(animPhase->loop)
						{
							animPhase->currentFrame = animPhase->endFrame;
						}
						else
						{
							animPhase->currentFrame = animPhase->startFrame;
							animPhase->_isDone = true;
						}

					}
					break;

				case AM_FWBOUNCE:


					break;

				case AM_BWBOUNCE:

					break;
				}
			}

		}

		int frameLeft = 0;
		int frameTop = 0;

		int frameXaframe = animPhase->frameWidth * (animPhase->currentFrame);
		int framesPerLine = (_srcWidth / animPhase->frameWidth);

		frameTop = animPhase->startPosY + (frameXaframe / _srcWidth) * animPhase->frameHeight;
		frameLeft = frameXaframe - (((animPhase->currentFrame ) / framesPerLine) * _srcWidth);


		SetRect(r, frameLeft, frameTop, frameLeft + animPhase->frameWidth, frameTop + animPhase->frameHeight);
		return;
	}

	SetRect(r, 0, 0, _srcWidth, _srcHeight);
}